﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace NearAop.Attributes
{
    [AttributeUsage(AttributeTargets.All)]
    public class ReflectionIdAttribute : Attribute
    {
        public ReflectionIdAttribute(int id) => Id = id;

        public int Id { get; }

        public static bool IsTarget(MemberInfo member, int id)
        {
            var datas = member.GetCustomAttributes(typeof(ReflectionIdAttribute), true);
            return datas.OfType<ReflectionIdAttribute>().Any(x => x.Id == id);
        }
    }

    [AttributeUsage(AttributeTargets.All)]
    public class ReflectionNameAttribute : Attribute
    {
        public ReflectionNameAttribute(string name)
        {
            Name = name;
        }

        public string Name { get; }

        public static bool IsTarget(MemberInfo member, string target)
        {
            return GetAttributes(member).Any(x => x.Name == target);
        }

        public static string GetTargetName(MemberInfo member)
        {
            return GetAttributes(member).FirstOrDefault()?.Name;
        }

        private static IEnumerable<ReflectionNameAttribute> GetAttributes(MemberInfo member)
        {
            return member.GetCustomAttributes(typeof(ReflectionNameAttribute), true)
                .OfType<ReflectionNameAttribute>();
        }
    }
}
